<template>
  <div>
    <el-tree
      ref="tree"
      :data="data"
      :props="defaultProps"
      default-expand-all
      node-key="id"
      :show-checkbox="true"
      :check-strictly="true"
      :default-checked-keys="idList"
      @node-click="handleNodeClick"
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="hCancel">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionsList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 权限列表
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 默认选中
      idList: []
    }
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    // 当前的角色权限
    async setRoleDetail() {
      try {
        const info = await getRoleDetail(this.curId)
        // console.log(info, 'info')
        this.idList = info.data.data.permIds
      } catch (error) {
        console.log(error)
      }
    },
    // 权限列表
    async setPermissionsList() {
      try {
        const res = await getPermissionsList()
        // console.log('getPermissionsList', res)
        this.data = tranListToTreeData(res.data.data)
        this.setRoleDetail()
        // 保存数据
      } catch (err) {
        console.log('getPermissionsList', err)
      }
    },
    // 取消按钮
    hCancel() {
      this.$emit('close')
    },
    // 确定按钮
    async hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        await assignPerm({ permIds, id: this.curId })
        //   console.log(res)
        this.$message.success('分配权限成功')
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },
    // 清空
    resetForm() {
      this.idList = []
    }
  }
}
</script>

<style>

</style>
